<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.model.ClassContainer" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%@ page import="schoolsource.model.*" %>

<%  
    SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
    String requestObject = (String) st.getValue(FormFields.TOKEN);
    ClassContainer classContainer = (ClassContainer) st.removeValue("classContainer"); 
    Item courses[][] = (Item[][]) st.removeValue("courseList");
    PeriodContainer periods[] = (PeriodContainer[]) st.removeValue("periodList");
//    Item classTeacher[] = (Item[]) st.removeValue("classTeacher");
    Item classTeacher[][] = (Item[][]) st.removeValue("classTeacher");
    Item studentList[][] = (Item[][]) st.removeValue("studentList");
    String editStatus = (String) st.removeValue(FormFields.EDITSTATUS);
    int editorPage = (new Integer((String) st.removeValue(FormFields.EDITORPAGE))).intValue();
    String displayMessage = (String) st.getValue("displayMessage"); 
    st.putValue("displayMessage", "");
	String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
	String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
%>



<HTML>

<HEAD>

  <title>Class Editor Form</title>
  
  <SCRIPT LANGUAGE="JavaScript">
  
      function init() {
        if ("<%= displayMessage %>" != "null" && "<%= displayMessage %>" != "") {
          top.frames.mainframe.notify("<%= displayMessage %>");
        }
      }
  
  </SCRIPT>

</HEAD>

    <SCRIPT LANGUAGE="JavaScript">
        
        function validateRequiredFields() {
            if (document.forms[0].<%= SchoolDBSQL.CLASSSCHOOLID %>.value == "") {
                alert("You must enter an ID for the class.");
                document.forms[0].<%= SchoolDBSQL.CLASSSCHOOLID %>.focus();
                return false;
            }
            else {
                return true;
            }
        }

        function cancelForm() {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.CANCEL %>";
            document.forms[0].submit();
        }

        function postProcessing(x) {
            if (validateRequiredFields()) {
                document.forms[0].<%= FormFields.SAVECLASS %>.value = x;
                document.forms[0].submit();
            }
        }
        
        function viewDetailedInfo(page, selectedItem) {
            top.frames.mainframe.document.forms[0].<%= FormFields.SUBLINK %>.value = selectedItem;
            top.frames.mainframe.submitPage(top.frames.mainframe.document, "<%= FormFields.EDITORPAGE %>", page);
            return false;

        }

    </SCRIPT>
    <body onLoad="init();
    	<% 
    	if (editStatus.equals(FormFields.VIEW) && classContainer != null) {
               out.print("top.frames.mainframe.highlightByID('" + classContainer.getClassAssignedID() + "');");
        }
        %>">

    <FORM NAME="classeditorform" METHOD="POST" onSubmit="return false;">
        <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.CLASSASSIGNEDID %>" <%= (st.removeValue("adding") == null) ? "onFocus=\"blur()\"" : "" %>
                    VALUE="<%= (classContainer!=null) ? classContainer.getClassAssignedID().toString() : "nothing there"%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="<%= FormFields.EDCLASSPAGE %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="<%= (editStatus.equals(FormFields.VIEW)) ? FormFields.EDITORPAGE : FormFields.SAVEACTION %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.EDITORPAGE %>" VALUE="<%= editorPage %>"> 
		<INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
		<INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">
        <%
            if (editStatus.equals(FormFields.VIEW)) {
                out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + FormFields.SUBLINK + "\" VALUE=\"\">");
            }
        %>
        
        <H2>Class Editor</H2><br>
            <TABLE WIDTH="600">
            <TR>
                <TH ALIGN="LEFT">Class ID</TH>
                <TD>
                <%
                  if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                    out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.CLASSSCHOOLID + "\" VALUE=\"" + classContainer.getClassSchoolID().toString() + "\">");
                  }
                  else {
                    out.println("                " + classContainer.getClassSchoolID().toString());
                  }
                %>
                </TD>
                
            </TR>

            <TR>
                <TH ALIGN="LEFT">Course</TH>
                <TD>
                  <%
                  if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                    out.println("                  <SELECT NAME=\"" + SchoolDBSQL.COURSEASSIGNEDID + "\">");
                    if (classContainer.getAssociatedCourse() == null) {
                        out.println("<OPTION VALUE=\"NONE\" SELECTED>None</OPTION>");
                    }
                    else {
                        out.println("<OPTION VALUE=\"NONE\">None</OPTION>");
                    }
                    for (int i =0; i < courses.length; i++){
                        if (classContainer.getAssociatedCourse() != null && courses[i][0].toString().equals(classContainer.getAssociatedCourse().getCourseAssignedID().toString())) {
                            out.println("<OPTION VALUE=\"" + courses[i][0].toString() + "\" SELECTED>");
                        }
                        else {
                            out.println("<OPTION VALUE=\"" + courses[i][0].toString() + "\">");
                        }
                        out.println(" " + courses[i][1].toString() +
                             " " + courses[i][2].toString() +
                             "</OPTION>");
                    }
                    out.println("</SELECT>");
                  }
                  else {
                    out.println("<a href=\"\" onClick=\"viewDetailedInfo('" + FormFields.EDCOURSEPAGE + "','" + classContainer.getAssociatedCourse().getCourseAssignedID().toString() + "');return false;\">" + classContainer.getAssociatedCourse().getCourseShortName().toString() + "</a>");
                  }
                  %>
                </TD>
                
            </TR>

            <TR>
                <TH ALIGN="LEFT">Time</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("          <SELECT NAME=\"" + SchoolDBSQL.CLASSTIME + "\">");
                            for (int i = 0; i < periods.length; i++) {
                                if (classContainer.getClassTime() != null && 
                                    classContainer.getClassTime().toString().equals(periods[i].getPeriodName().toString())) {
                                    
                                    out.print("<OPTION VALUE=\"" + periods[i].getPeriodName() + "\" SELECTED>");
                                    
                                }
                                else {
                                
                                    out.println("<OPTION VALUE=\"" + periods[i].getPeriodName() + "\">");
                                
                                }
			        out.print(periods[i].getPeriodName());
			        out.println("</OPTION>");
                            }
                            out.println("</SELECT>");
                        }
                        else {
                            out.println(classContainer.getClassTime().toString());
                        }
                    %>
                </TD>
            </TR>
            <%
                if (editStatus.equals(FormFields.VIEW)) {
                    out.println("            <TR>");
                    out.println("                <TH ALIGN=\"LEFT\">Teacher</TH>");
                    out.println("                  <TD>");
                    for (int i = 0; i < classTeacher.length; i++) {
                        out.println("                <a href=\"\" onClick=\"viewDetailedInfo('" + FormFields.EDTEACHERPAGE + "','" + classTeacher[i][0] + "');return false;\">" + classTeacher[i][2] + ", " + classTeacher[i][1] + "</a></TD>");
                    }
                    out.println("              </TD>");
                    out.println("            </TR>");
                    out.println("            <TR>");
                    out.println("                <TH VALIGN=\"TOP\" ALIGN=\"LEFT\">Enrolled Students</TH>");
                    out.println("            </TR>");
                    for (int i = 0; i < studentList.length; i++) {
                        out.println("            <TR>");
                        out.println("              <TD>&nbsp;</TD>");
                        out.println("              <TD><a href=\"\" onClick=\"viewDetailedInfo('" + FormFields.EDSTUDENTPAGE + "','" + studentList[i][0] + "');return false;\">" + studentList[i][2] + ", " + studentList[i][1] + "</a></TD>");                     
                        out.println("            </TR>");
                    }
                }
            %>
            <TR>
                <TD COLSPAN=2>&nbsp;</TD>
            </TR>
            <TR>
                <TD COLSPAN=2>&nbsp;</TD>
            </TR>
            <TR>
                <%
                    if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                        out.println("                <TD><INPUT NAME=\"" + FormFields.SAVECLASS  + "\" TYPE=\"BUTTON\" VALUE=\"Save\" onClick=\"postProcessing('Save')\"></TD>");
                        out.println("                <TD><INPUT NAME=\"cancel\" TYPE=\"BUTTON\" VALUE=\"Cancel\" onClick='cancelForm()'></TD>");
                    }
                %>
            </TR>

        </TABLE>

    </FORM>
</HTML>

